package com.cfpl.admin_service.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cfpl.DTO.PageQueryConditionDTO;
import com.cfpl.admin_service.service.PermissionService;
import com.cfpl.project_common.enums.CodeEnum;
import com.cfpl.project_common.exception.BusException;
import com.cfpl.project_common.pojo.Permission;
import com.cfpl.project_common.result.BaseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Auther: LYP
 * @Date: 2023/10/11 - 11:21
 * @Description: com.cfpl.admin_service.controller
 * @version: 1.0
 */
@RestController
@Slf4j
@RequestMapping("/admin/permission")
@Api(tags = "管理员权限相关接口")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;


    /**
     * 分页查询权限权限 TestSuccess
     * @param pageConditionDTO 当前页
     * @param pageConditionDTO 每页显示条数
     * @return
     */
    @GetMapping("/pageQueryPermission")
    @ApiOperation("分页查询权限权限")
    public BaseResult<Page<Permission>> pageQueryPermission( PageQueryConditionDTO pageConditionDTO){
        Page<Permission> page = permissionService.page(new Page<>(pageConditionDTO.getCurrentPage(), pageConditionDTO.getPageSize()), null);
        return BaseResult.ok(page);
    }

    /**
     * 添加管理员权限 TestSuccess
     * @param permission 权限对象
     * @return status
     */
    @PostMapping("/insertAdminPermission")
    @ApiOperation("添加管理员权限")
    public BaseResult insertAdminPermission(@RequestBody Permission permission){
        permissionService.insertPermission(permission);
        return BaseResult.ok();
    }

    /**
     * 删除权限 TestSuccess
     * @param permissionIds 权限编号
     * @return return
     */
    @DeleteMapping("/removeAdminPermission")
    @ApiOperation("删除管理员权限(可批量可单个)")
    public BaseResult removeAdminPermission(@RequestParam List<Long> permissionIds){
        if (permissionIds == null || permissionIds.size() < 1){
            throw new BusException(CodeEnum.PARAMETER_ERROR);
        }
        permissionService.removePermissionsById(permissionIds);
        return BaseResult.ok();
    }

    /**
     * 根据ID查询权限信息 TestSuccess
     * @param permissionId 权限ID
     * @return
     */
    @GetMapping("/queryPermissionDetailInfo/{id}")
    @ApiOperation("根据ID查询权限信息")
    public BaseResult<Permission> queryPermissionDetailInfo(@PathVariable("id") Long permissionId){
        if (permissionId == null){
            throw new BusException(CodeEnum.PARAMETER_ERROR);
        }
        Permission permission = permissionService.getById(permissionId);
        return BaseResult.ok(permission);
    }

    /**
     * 修改权限信息 TestSuccess
     * @param permission 权限信息
     * @return return
     */
    @PostMapping("/updatePermissionInfo")
    @ApiOperation("修改权限信息")
    public BaseResult updatePermissionInfo(@RequestBody Permission permission){
        permissionService.updatePermissionInfo(permission);
        return BaseResult.ok();
    }

    /**
     * 查询权限列表 TestSuccess
     * @return
     */
    @GetMapping("/queryAllPermission")
    @ApiOperation("查询权限列表")
    public BaseResult<List<Permission>> queryAllPermission(){
        List<Permission> permissionList = permissionService.list();
        return BaseResult.ok(permissionList);
    }


}
